"use strict";
const assert = require("assert");

const cfg = require("./config/server");
const tool = require("./lib/common");
const signature = require("./lib/signature");

const defaultKey = cfg.key;
const defaultSecret = cfg.secret;
const defaultServer = cfg.idcServer.url;
const defaultVersion = cfg.idcServer.version;
const validateDomain = function(domain) {
	assert.ok(domain, "domain can't be null");
	assert.ok(domain.id_name, "domain id_name can't be null");
	assert.ok(domain.access_endpoint, "domain access_endpoint can't be null");
	assert.ok(domain.website_endpoint, "domain website_endpoint can't be null");
	assert.ok(domain.display_name, "domain display_name can't be null");
};

const getSignature = function(partOfUrl, time, queryString) { //创建防伪签名
	const rawSignature = `${this.secret}&${time}&/${this.version}/${partOfUrl}`;
	return signature.encrypt(rawSignature);
};

const getRequestOptions = function(partOfUrl, queryString = "", method = "GET", body = undefined) { //根据签名创建HTTP头部
	const time = Date.now();
	const createOption = signature => {
		const param = {
			key: this.key,
			time: time,
			token: signature
		};
		const option = {
			url: `${this.idcServer}/${this.version}/${partOfUrl}` + (queryString ? `?${queryString}` : ""),
			method: method,
			headers: tool.getHeaders(param),
			body: body,
			json: true
		};
		if (!body) delete option.body;
		return option;
	};
	return getSignature.call(this, partOfUrl, time, queryString).then(createOption);
};

const createDomain = function(domainInfo) {
	validateDomain(domainInfo);
	const partOfUrl = `admin/domains`;
	const doCreateDomain = options => {
		console.log("创建domain的options是", tool.inspect(options, 2));
		return tool.sendRequest(options);
	};
	return getRequestOptions.call(this, partOfUrl, "", "POST", domainInfo).then(doCreateDomain);
};

const queryDomainById = function(domainId) {
	assert.ok(domainId, "domainId can't be null");
	const doQueryDomain = options => {
		console.log("查询domain的options是", tool.inspect(options, 2));
		return tool.sendRequest(options);
	};
	const partOfUrl = "admin/domains";
	const queryString = `id_name=${domainId}`;
	return getRequestOptions.call(this, partOfUrl, queryString).then(doQueryDomain);
};
const deleteDomainById = function(domainId) {
	assert.ok(domainId, "domainId can't be null");
	const doDeleteDomain = options => {
		console.log("删除domain的options是", tool.inspect(options, 2));
		return tool.sendRequest(options);
	};
	const partOfUrl = `admin/domains/${domainId}`;
	return getRequestOptions.call(this, partOfUrl, "", "DELETE").then(doDeleteDomain);
};

const createLicense = function(licenseInfo) {
	const partOfUrl = `admin/domains/${licenseInfo.domainId}/licenses`;
	const doCreateLicense = options => {
		console.log("创建license的options是", tool.inspect(options, 2));
		return tool.sendRequest(options);
	};
	return getRequestOptions.call(this, partOfUrl, "", "POST", licenseInfo).then(doCreateLicense);
};

const initDomainModule = function(key, secret, idcServer, version) {
	key = key || defaultKey;
	secret = secret || defaultSecret;
	idcServer = tool.removeTailSlash(idcServer) || defaultServer;
	version = version || defaultVersion;
	const ctx = {
		key,
		secret,
		idcServer,
		version
	};
	return {
		createDomain: createDomain.bind(ctx),
		queryDomainById: queryDomainById.bind(ctx),
		deleteDomainById: deleteDomainById.bind(ctx),
		createLicense: createLicense.bind(ctx)
	};
};

module.exports = {
	domain: initDomainModule
};
